package cn.itcast.form;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

public class RegisterForm {
	private String username;
	private String password;
	private String password2;
	private String email;
	private String birthday;
	private String nicname;
	private String yzm;
	private String yzm2;
	private Map<String, String> errors = new HashMap<String, String>();
	
	public Map<String, String> getErrors() {
		return errors;
	}
	public void setErrors(Map<String, String> errors) {
		this.errors = errors;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getNicname() {
		return nicname;
	}
	public void setNicname(String nicname) {
		this.nicname = nicname;
	}
	
	
	public String getYzm() {
		return yzm;
	}
	public void setYzm(String yzm) {
		this.yzm = yzm;
	}
	public String getYzm2() {
		return yzm2;
	}
	public void setYzm2(String yzm2) {
		this.yzm2 = yzm2;
	}
	
	public boolean validate() {
		boolean isOK = true;
		if(this.username==null || this.username.trim().equals("")) {
			isOK = false;
			errors.put("username","用户名不能为空");
		}
		else if(!this.username.matches("[A-Za-z]{3,8}")) {
			isOK = false;
			errors.put("username", "用户名不符合规范");
		}
		
		if(this.password==null || this.password.trim().equals("")) {
			isOK = false;
			errors.put("password", "密码不能为空");
		}
		else if(!this.password.matches("\\d{6,12}")) {
			isOK = false;
			errors.put("password", "密码不符合规范");
		} 
		
		if(this.password2==null || this.password2.trim().equals("")) {
			isOK = false;
			errors.put("password2", "确认密码不能为空");
		}
		else if(!this.password.equals(password2)) {
			isOK = false;
			errors.put("password2", "两次输入密码不一致");
		}
		
		if(this.email==null || this.email.trim().equals("")) {
			isOK = false;
			errors.put("email","邮箱不能为空");
		}
		else if(!this.email.matches("\\w+@\\w+(\\.\\w+)+")) {
			isOK = false;
			errors.put("email", "邮箱不符合规范");
		}
		
		if(this.birthday!=null && !this.birthday.trim().equals("")) {
			try{
				DateLocaleConverter dlc = new DateLocaleConverter();
				dlc.convert(this.birthday, "yyyy-MM-dd");
			}catch(Exception e) {
				isOK = false;
				errors.put("birthday", "日期格式不正确");
			}
		}
		
		if(this.nicname==null || this.nicname.trim().equals("")) {
			isOK = false;
			errors.put("nicname", "昵称不能为空");
		}
		else if (!this.nicname.matches("[\u4e00-\u9fa5]+")) {
			isOK = false;
			errors.put("nicname", "昵称必须为汉字 ");
		}
		
		if(this.yzm==null || this.yzm.trim().equals("")) {
			isOK = false;
			errors.put("check", "验证码不能为空！");
		}
		else if(!this.yzm.equals(this.yzm2)) {
			isOK = false;
			errors.put("check", "验证码错误！");
		}
		return isOK;
	}
}
